#ifndef _______WARD____GLSCROLLINGTEXT_H
#define _______WARD____GLSCROLLINGTEXT_H

#include "driver.h"
#include <fstream>

static void writeTiltedText(GLfloat x,GLfloat y, GLfloat z, GLfloat angle, GLfloat x_scale,GLfloat y_scale, string text)
{
	glPushMatrix();
		glTranslatef(x,y,0);
		glScalef(x_scale,-y_scale,0.0);
		glRotatef(angle, 1.0, 0.0, 0.0);
			for(unsigned int q=0;q<text.size();++q)
			{glutStrokeCharacter(GLUT_STROKE_ROMAN,text[q]);}
	glPopMatrix();
}

class ScrollText
{
	public:
		ScrollText(string t,GLfloat m) :text(t),mult(m) {posy = 630; posx = -25; posz = 0;}
		GLfloat getx() { return posx;}
		GLfloat gety() { return posy;}
		GLfloat getz() { return posz;}
		string getText() { return text;}

		void incSpeed(GLfloat amount) {mult+=amount;}
		void decSpeed(GLfloat amount) {mult-=amount; if(mult<0){mult=0;}}

		void update() { posx += mult*0.55*(posy/600); posy -= abs((mult*0.9*(posy/600)));}
	private:
		string text;
		GLfloat mult;
		GLfloat posx, posy, posz;
};

class glScrollingText
{
	public:
		glScrollingText(string file,GLfloat m, bool loop = 0)
			: looping(loop), mult(m)
		{
			this->pushfile(file);
			top = 1;
			bottom = 0;
		}

		void pushfile(string fname)
		{
			ifstream ifs(fname.c_str());
			string s;

			while(!ifs.eof())
			{
				getline(ifs, s);
				if(!ifs.eof())
					this->push(s);
			}
		}

		void push(string text)  { lla.push_back(ScrollText(text,mult)); }

		void draw()
		{
			for(int i = bottom; i < top; i++)
			{
				glLineWidth((lla[i].gety()/(float)800)*3);
				writeTiltedText(lla[i].getx(), lla[i].gety(), lla[i].getz(), (90-(lla[i].gety()/2400)*90), 0.2*lla[i].gety()/(float)400, 0.2*lla[i].gety()/(float)400, lla[i].getText());
				glLineWidth(1);
				lla[i].update();
			}

			if(lla[bottom].gety() < 100)
			{
				if(looping)
					lla.push_back(ScrollText(lla[bottom].getText(),mult));	
				if(bottom < top-1)
					bottom++;
			}

			if(lla[top-1].gety() < 575)
				if(top != lla.size())
					top++;
		}

		void incSpeed(GLfloat amount=0.5)
		{
			mult+=amount;

			for(unsigned int i=0;i<lla.size();++i)
			{lla[i].incSpeed(amount);}
		}
		
		void decSpeed(GLfloat amount=0.5)
		{
			mult-=amount;
			if(mult<0){mult=0;}

			for(unsigned int i=0;i<lla.size();++i)
			{lla[i].decSpeed(amount);}
		}

	private:
		vector <ScrollText> lla;
		int top;	
		int bottom;
		bool looping;
		GLfloat mult;
};

#endif